/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.bandwidth;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class FilterTableModel extends AbstractTableModel{
    ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);
    Vector filters;

    public FilterTableModel(Vector _filters) {
        this.filters = _filters;
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM JNA_FILTER ");
        Connection con = Program.connection;
        Statement stm;
        ResultSet rs;
        int i = 0;
        try {
            stm = con.createStatement();
            rs = stm.executeQuery(sql.toString());
            while(rs.next()){
                Timestamp time = rs.getTimestamp(2);
                String source_add = rs.getString(3);
                String source_subnet = rs.getString(4);
                String dest_add = rs.getString(5);
                String dest_subnet = rs.getString(6);
                String protocol_name = rs.getString(7);
                int source_port = rs.getInt(8);
                int dest_port = rs.getInt(9);
                boolean isActive = rs.getBoolean(10);
                String description = rs.getString(11);
                FilterInfo filter = new FilterInfo(source_add, dest_add, source_subnet, dest_subnet,
                        protocol_name, source_port, dest_port, description, isActive, time);
                filters.addElement(filter);
                fireTableRowsInserted(i, i);
                i++;
            }
        } catch (SQLException ex) {
        }
    }


    public int getRowCount() {
        return filters.size();
    }

    public int getColumnCount() {
        return 11;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        FilterInfo filter = (FilterInfo)filters.elementAt(rowIndex);
        switch(columnIndex){
            case 0:{
                return new Long(rowIndex + 1);
            }
            case 1: {
                Date date = new Date(filter.getTime().getTime());
                return  date;
            }
            case 2: {
                return filter.getSourceAdd();
            }
            case 3: {
                return filter.getSourceSubnet();
            }
            case 4: {
                return filter.getDestAdd();
            }
            case 5: {
                return filter.getDestSubnet();
            }
            case 6: {
                return filter.getProtocolName();
            }
            case 7: {
                return filter.getSourcePort();
            }
            case 8: {
                return filter.getDestPort();
            }
            case 9: {
                return filter.getIsActive();
            }
            case 10: {
                return filter.getDescription();
            }
            default:{
                return null;
            }
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public String getColumnName(int column) {
        if(column == 0) return resourceManager.getString("index");
        if(column == 1) return resourceManager.getString("time");
        if(column == 2) return resourceManager.getString("sourceAddress");
        if(column == 3) return resourceManager.getString("sourceSubnet");
        if(column == 4) return resourceManager.getString("destAddress");
        if(column == 5) return resourceManager.getString("destSubnet");
        if(column == 6) return resourceManager.getString("protocolType");
        if(column == 7) return resourceManager.getString("sourcePort");
        if(column == 8) return resourceManager.getString("destPort");
        if(column == 9) return resourceManager.getString("actived");
        if(column == 10) return resourceManager.getString("description");
        return "";
    }

    @Override
    public Class getColumnClass(int c){
        switch(c){
            case 0: return Long.class;
            case 1: return Date.class;
            case 2: return String.class;
            case 3: return String.class;
            case 4: return String.class;
            case 5: return String.class;
            case 6: return String.class;
            case 7: return Integer.class;
            case 8: return Integer.class;
            case 9: return Boolean.class;
            case 10: return String.class;
        }
        return null;
    }

}
